package com.bocai.dahuatech.ui.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.PointBean;
import com.bocai.dahuatech.Bean.PointListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.ui.adapter.MyPointAdapter;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的华积分
 */
public class NewMyPointActivity extends BaseActivity {

    @Bind(R.id.ll_title_right)
    LinearLayout llTitle;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.lv_mypoint)
    ListView lvMyPoint;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    TextView totalPoint;
    TextView canusePoint;
    TextView frozenPoint;
    PointBean pointBean;
    PointListBean pointlistBean;
    List<PointListBean.ContentEntity> list=new ArrayList<>();
    MyPointAdapter adapter;
    @Bind(R.id.ll_mypoint_nocontent)
    LinearLayout llNoContent;
    View view;
    int page=1;
    @Override
    protected int contentView() {
        return R.layout.activity_my_point_new;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.mepoint);
        if(SharePrefencesUtil.getFAccessFlag(this)==2){
            llNoContent.setVisibility(View.GONE);
            llTitle.setVisibility(View.GONE);
            etSearch.setHint(R.string.orderserchhint);
            view= LayoutInflater.from(this).inflate(R.layout.my_point_header,null);
            lvMyPoint.addHeaderView(view);
            totalPoint= (TextView) view.findViewById(R.id.tv_totalpoint);
            canusePoint= (TextView) view.findViewById(R.id.tv_canusepoint);
            frozenPoint= (TextView) view.findViewById(R.id.tv_frozenpoint);
            getPoint();
            initEvent();
        }else{
            llNoContent.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getPoint();
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    page = 1;
                    getPointlist(etSearch.getText().toString());
                }
                return false;
            }
        });
        lvMyPoint.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    // 当不滚动时
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        // 判断滚动到底部
                        if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
                            page++;
                            getPointlist("");
                        }
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
            }
        });
    }

    private void getPoint() {
        showLoading();
        getTestApi().getPoint(SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    private void getPointlist(String search) {
        getTestApi().getPointList(SharePrefencesUtil.getCustomerCode(this), search, page)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observerlist);
    }
    Observer<BaseBean> observerlist=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            swipeRefreshLayout.setRefreshing(false);
            Log.e("result", "zhang");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            swipeRefreshLayout.setRefreshing(false);
            if(baseBean.getReturnNo().equals("1"))
            {

                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                pointlistBean=new Gson().fromJson(result,PointListBean.class);
//                setHeaderData();
                if(page==1){
                    adapter=new MyPointAdapter(NewMyPointActivity.this,pointlistBean.getContent());
                    lvMyPoint.setAdapter(adapter);
                }else{

                    adapter.addDatas(pointlistBean.getContent());
                }

            }else{
                showToast(baseBean.getReturnInfo());
            }
        }
    };
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("result","zhang");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            if(baseBean.getReturnNo().equals("1")) {

                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result", result);
                pointBean=new Gson().fromJson(result,PointBean.class);
//                Double fcoin=pointBean.getContent().get(0).getFCoin();
                totalPoint.setText(getResources().getText(R.string.pointall)+pointBean.getContent().get(0).getFCoin());
                canusePoint.setText(getResources().getText(R.string.pointuse)+ pointBean.getContent().get(0).getFAvailableCoin());
                frozenPoint.setText(getResources().getText(R.string.pointfrozen)+pointBean.getContent().get(0).getFColdCoin());
                getPointlist("");
            }
        }
    };

}
